<?php

namespace app\models;

use Yii;
use yii\web\IdentityInterface;

/**
 * This is the model class for table "{{%user}}".
 *
 * @property string $openid
 * @property string $nickname
 * @property string $avatar
 * @property string $realname
 * @property string $phone
 * @property string $code
 * @property string $bank
 * @property string $img1
 * @property string $img2
 * @property string $img3
 * @property integer $createdAt
 */
class User extends \yii\db\ActiveRecord implements IdentityInterface
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['openid', 'nickname', 'avatar', 'createdAt'], 'required'],
            [['createdAt'], 'integer'],
            [['openid'], 'string', 'max' => 40],
            [['nickname', 'phone'], 'string', 'max' => 20],
            [['avatar'], 'string', 'max' => 255],
            [['realname'], 'string', 'max' => 10],
            [['code'], 'string', 'max' => 14],
            [['bank'], 'string', 'max' => 4],
            [['img1', 'img2', 'img3'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'openid' => 'Openid',
            'nickname' => '昵称',
            'avatar' => '头像',
            'realname' => '姓名',
            'phone' => '电话',
            'code' => '验证码',
            'bank' => '银行',
            'img1' => '身份证正面',
            'img2' => '身份证反面',
            'img3' => '银行卡反面',
            'createdAt' => '授权时间',
        ];
    }

    public function beforeValidate()
    {
        if ($this->isNewRecord) {
            $this->createdAt = time();
        }
        if (empty($this->avatar)) {
            $this->avatar = 'http://7xthiz.com1.z0.glb.clouddn.com/avatar.jpg';
        }
        return parent::beforeValidate();
    }


    /**
     * Finds an identity by the given ID.
     * @param string|integer $id the ID to be looked for
     * @return IdentityInterface the identity object that matches the given ID.
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
     */
    public static function findIdentity($id)
    {
        return static::findOne($id);
    }

    /**
     * Finds an identity by the given token.
     * @param mixed $token the token to be looked for
     * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
     * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
     * @return IdentityInterface the identity object that matches the given token.
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        return null;
    }

    /**
     * Returns an ID that can uniquely identify a user identity.
     * @return string|integer an ID that uniquely identifies a user identity.
     */
    public function getId()
    {
        return $this->openid;
    }

    /**
     * Returns a key that can be used to check the validity of a given identity ID.
     *
     * The key should be unique for each individual user, and should be persistent
     * so that it can be used to check the validity of the user identity.
     *
     * The space of such keys should be big enough to defeat potential identity attacks.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
     * @return string a key that is used to check the validity of a given identity ID.
     * @see validateAuthKey()
     */
    public function getAuthKey()
    {
        return md5($this->openid);
    }

    /**
     * Validates the given auth key.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
     * @param string $authKey the given auth key
     * @return boolean whether the given auth key is valid.
     * @see getAuthKey()
     */
    public function validateAuthKey($authKey)
    {
        return md5($this->openid) == $authKey;
    }

    public function getWin()
    {
        return $this->hasOne(Win::className(), ['openid' => 'openid']);
    }

    public function getPlaylogs()
    {
        return $this->hasMany(Playlog::className(), ['openid' => 'openid']);
    }
}
